import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author wuxj
 * @date 2020/2/10 0:01
 */
public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    /**
     * RUN THIS
     */
    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("wuxj");
        gc.setOpen(false);//生成后是否打开资源管理器
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setFileOverride(true); //重新生成时文件是否覆盖
        gc.setIdType(IdType.ASSIGN_ID); //主键策略
        gc.setBaseResultMap(true);//初始话xmlBaseResultMap
        mpg.setGlobalConfig(gc);

        String moduleName = scanner("模块名");

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://101.200.195.76:3306/skyworth-" + moduleName + "?useUnicode=true&serverTimezone=GMT&useSSL=false&characterEncoding=utf8");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("skyworth");
        dsc.setPassword("G1ZvvLKmx3JDGSQ3");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent("com.skyworth");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {

            }
        };

        List<FileOutConfig> focList = new ArrayList<>();

        focList.add(new FileOutConfig("/templates/my/AddReqDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/pojo/dto/" + tableInfo.getEntityName().toLowerCase() + "/req/" + tableInfo.getEntityName() + "AddReqDTO" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/GetResDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/pojo/dto/" + tableInfo.getEntityName().toLowerCase() + "/res/" + tableInfo.getEntityName() + "GetResDTO" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/ListReqDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/pojo/dto/" + tableInfo.getEntityName().toLowerCase() + "/req/" + tableInfo.getEntityName() + "ListReqDTO" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/ListResDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/pojo/dto/" + tableInfo.getEntityName().toLowerCase() + "/res/" + tableInfo.getEntityName() + "ListResDTO" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/UpdateReqDTO.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/pojo/dto/" + tableInfo.getEntityName().toLowerCase() + "/req/" + tableInfo.getEntityName() + "UpdateReqDTO" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/api.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/api/" + tableInfo.getEntityName() + "Api" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/client.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/client/" + tableInfo.getEntityName() + "Client" + StringPool.DOT_JAVA;
            }
        });
        focList.add(new FileOutConfig("/templates/my/fallback.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return projectPath + "/src/main/java/com/skyworth/" + moduleName + "api/client/fallback/" + tableInfo.getEntityName() + "Fallback" + StringPool.DOT_JAVA;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig());

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();

        strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setSuperEntityClass("com.skyworth.core.entity.AbstractEntity");
        strategy.setEntityLombokModel(true);
        ; // lombok 模型
        strategy.setSuperControllerClass("com.skyworth.common.base.controller.AbstractController");
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        mpg.setStrategy(strategy);
        // 选择 freemarker 引擎需要指定如下加，注意 pom 依赖必须有！
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
}
